package com.swm.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.swm.mapper.StockMapper;
import com.swm.pojo.Data;
import com.swm.pojo.Stock;
import org.apache.ibatis.session.SqlSession;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.swm.util.ConditionUtilDC.*;
import static com.swm.util.getStockDayData4.days;
import static com.swm.util.getStockDayData4.times;

/**
 * @author 师文明
 * @data 2024/12/23 下午3:11
 **/
public class getStockDayData445 {

    public static int allup;
    public static double allupnum;
    public static double sum;
    public static double sum3;
    public static double sum5;
    public static double sum10;
    public static double sum20;
    public static int num0;
    public static int numu;
    public static int numd;
    public static int numu3;
    public static int numd3;
    public static int numu5;
    public static int numd5;
    public static int numu10;
    public static int numd10;
    public static int numu20;
    public static int numd20;
    public static int ban = 0;
    static SqlSession sqlSession = SqlSessionUtil.openSession();
    static StockMapper mapper = sqlSession.getMapper(StockMapper.class);
    private static ArrayList<Data> dataList = new ArrayList<Data>();

    private static ArrayList<String> dateList = new ArrayList<String>();

    public static List<Stock> getStcokList() {
        List<Stock> stocks = mapper.selectAllStock();
        JSONObject jsonObject = new JSONObject();

        for (Stock stock : stocks) {
            String code = stock.getCode();
            int type = stock.getType();
            jsonObject.put(code, type);

        }
        return stocks;
    }

    public static void main(String[] args) throws Exception {

        ExecutorService executorService = Executors.newFixedThreadPool(50);
        //获取所有股票列表
        List<Stock> stcokList = getStcokList();
        for (Stock stock : stcokList) {
            executorService.execute(() -> {
                try {
                    getData(stock.getCode(), stock.getType());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
        }

        Thread.sleep(times);
        double v0 = 0;
        if (allup != 0) {
            v0 = allupnum / allup;
        }
        DecimalFormat df = new DecimalFormat("#0.000");
        System.out.println(("上涨次数" + numu + "  下跌次数" + numd));
        System.out.println(("平均涨幅：" + v0 + "上涨概率 " + df.format((double) numu / allup)));

        double v = 0;
        double v3 = 0;
        double v5 = 0;
        double v10 = 0;

        if (num0 != 0 && sum != 0) {
            v = sum / num0;
        }
        if (num0 != 0 && sum3 != 0) {
            v3 = sum3 / num0;
        }
        if (num0 != 0 && sum5 != 0) {
            v5 = sum5 / num0;
        }
        if (num0 != 0 && sum10 != 0) {
            v10 = sum10 / num0;
        }

  /*      System.out.println(("一天后平均涨幅：" + v));
        System.out.println(("3天后平均涨幅：" + v3));
        System.out.println(("5天后平均涨幅：" + v5));
        System.out.println(("10天后平均涨幅：" + v10));*/


        System.out.println(("一天后涨的次数：" + numu + "  跌的次数：" + numd + "  上涨概率： " + df.format((double) numu / num0)) + "  平均涨幅：" + df.format(v));
        System.out.println(("3天后涨的次数：" + numu3 + "  跌的次数：" + numd3 + "  上涨概率： " + df.format((double) numu3 / num0)) + "  平均涨幅：" + df.format(v3));
        System.out.println(("5天后涨的次数：" + numu5 + "  跌的次数：" + numd5 + "  上涨概率： " + df.format((double) numu5 / num0)) + "  平均涨幅：" + df.format(v5));
        System.out.println(("10天后涨的次数：" + numu10 + "  跌的次数：" + numd10 + "  上涨概率： " + df.format((double) numu10 / num0)) + "  平均涨幅：" + df.format(v10));
        System.out.println("最近" + days + "天  前一天涨幅6-8");

        System.out.println("板数：" + ban);
        System.out.println(dateList.size());
        // 关闭线程池，以优雅的方式

        // executorService.shutdown();
    }

    public static void getData(String code, int type) throws Exception {

        String data = getWebData(code, type);
        if (data == null) {
            return;
        }

        JSONObject jsonObject = JSONObject.parseObject(data);
        if (jsonObject.get("data") != null) {
            JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.get("data").toString());
            String name = jsonObject1.get("name").toString();
            JSONArray klines = JSONArray.parse(jsonObject1.get("klines").toString());
            if (klines.size() > days + 150) {
                for (int i = 2; i < days + 2; i++) {

                    //昨日涨幅
                    double v = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[8]);
                    //前日涨幅
                    double vv1 = Double.parseDouble(klines.get(klines.size() - i - 1).toString().split(",")[8]);
                    //大前日涨幅
                    double vv2 = Double.parseDouble(klines.get(klines.size() - i - 2).toString().split(",")[8]);
                    //大前日涨幅
                    double vv3 = Double.parseDouble(klines.get(klines.size() - i - 3).toString().split(",")[8]);
                    //大前日涨幅
                    double vv4 = Double.parseDouble(klines.get(klines.size() - i - 4).toString().split(",")[8]);


                    JSONObject lines = getLines(klines, i);
                    double f1 = Double.parseDouble(lines.get("f1").toString());
                    double te1 = Double.parseDouble(lines.get("te1").toString());
                    double tw1 = Double.parseDouble(lines.get("tw1").toString());
                    double th1 = Double.parseDouble(lines.get("th1").toString());
                    double six1 = Double.parseDouble(lines.get("six1").toString());

                    JSONObject lines2 = getLines(klines, i + 1);
                    double f2 = Double.parseDouble(lines2.get("f1").toString());
                    double te2 = Double.parseDouble(lines2.get("te1").toString());
                    double tw2 = Double.parseDouble(lines2.get("tw1").toString());
                    double th2 = Double.parseDouble(lines2.get("th1").toString());
                    double six2 = Double.parseDouble(lines2.get("six1").toString());
                    //开盘价
                    double k = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[1]);
                    //收盘价
                    double s = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[2]);

                    //最高价
                    double g = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[3]);
                    //最低价
                    double d = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[4]);

                    //当天振幅
                    double v6 = (g - d) / k;

                    //一阳穿两线 5 10
                    boolean bb = k < f1 && k < te1;
                    boolean b0 = s > f1 && s > te1;
                    //一阳穿三线 5 10 30
                    boolean b = k < f1 && k < te1 && k < th1;
                    boolean b1 = s > f1 && s > te1 && s > th1;
                    //一阳穿四线30 5 10 20 30
                    boolean b2 = k < f1 && k < te1 && k < tw1 && k < th1;
                    boolean b3 = s > f1 && s > te1 && s > tw1 && s > th1;

                    //一阳穿四线60 5 10 20 60
                    boolean b4 = k < f1 && k < te1 && k < tw1 && k < six1;
                    boolean b5 = s > f1 && s > te1 && s > tw1 && s > six1;

                    //一阳穿五线 5 10 20 30 60
                    boolean b6 = k < f1 && k < te1 && k < tw1 && k < th1 && k < six1;
                    boolean b7 = s > f1 && s > te1 && s > tw1 && s > th1 && s > six1;


                    boolean bf = k < f1 && s > f1;
                    boolean bte = k < te1 && s > te1;
                    boolean btw = k < tw1 && s > tw1;
                    boolean bth = k < th1 && s > th1;
                    boolean bsix = k < six1 && s > six1;
                    int flag = 0;
                    if (bf) {
                        flag++;
                    }
                    if (bte) {
                        flag++;
                    }
                    if (btw) {
                        flag++;
                    }
                    if (bth) {
                        flag++;
                    }
                    if (bsix) {
                        flag++;
                    }

                    //近80天价格处于低位
                    //boolean price = getPrice(klines, i+3);
                    //近30天价格处于高位
                    boolean insertPrice = getInsertPrice(klines, i);
                    //近几日是否放量
                    // boolean moneyUp = getMoneyUp(klines, 5, i);

                    //近80天成交量处于高位
                    //boolean money = getMoney(klines, i);

                    //今日成交额
                    double v0 = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[5]);
                    //昨日成交额
                    double v1 = Double.parseDouble(klines.get(klines.size() - i - 1).toString().split(",")[5]);
                    double v4 = v0 / v1;


                    boolean c1 = f1 < te1 && te1 < tw1 && tw1 < th1 && th1 < six1;

                    double maxline = 0.0;
                    double minline = 1000000000;
                    maxline = Math.max(maxline, f1);
                    maxline = Math.max(maxline, te1);
                    maxline = Math.max(maxline, tw1);
                    maxline = Math.max(maxline, th1);
                    maxline = Math.max(maxline, six1);

                    minline = Math.min(minline, f1);
                    minline = Math.min(minline, te1);
                    minline = Math.min(minline, tw1);
                    minline = Math.min(minline, th1);
                    minline = Math.min(minline, six1);
                    double v9 = (maxline - minline) / s;


                    //今日成交额
                    double v8 = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[5]);
                    //昨日成交额
                    double v61 = Double.parseDouble(klines.get(klines.size() - i - 1).toString().split(",")[5]);
                    //前日成交额
                    double v62 = Double.parseDouble(klines.get(klines.size() - i - 1).toString().split(",")[5]);
                    //大前日成交额
                    double v66 = Double.parseDouble(klines.get(klines.size() - i - 1).toString().split(",")[5]);
                    double v71 = v8 / v66;
                    double v72 = v61 / v66;
                    double v73 = v62 / v66;


                    boolean b8 = f1 > te1 && te1 /*> tw1 && tw1 */ > th1 /*&& th1 > six1*/;
                    boolean b9 = /*f2 < six2 ||*/f2 < th2 ||/*f2<tw2||*/f2 < te2 ||/*te2<six2||*/te2 < th2/*||*//*te2<tw2||*//*tw2<six2||tw2<th2||th2<six2*/;

                    boolean b10 = f1 > te1 && f2 < te2;
                    boolean b11 = f1 > tw1 && f2 < tw2;
                    boolean b12 = f1 > th1 && f2 < th2;
                    boolean b13 = f1 > six1 && f2 < six2;

                    boolean b14 = te1 > tw1 && te2 < tw2;
                    boolean b15 = te1 > th1 && te2 < th2;
                    boolean b16 = te1 > six1 && te2 < six2;

                    boolean b17 = tw1 > th1 && tw2 < th2;
                    boolean b18 = tw1 > six1 && tw2 < six2;
                    boolean b19 = th1 > six1 && th2 < six2;

                    //20天内最大
                    //double maxPrice = getMaxPrice(klines, i+2, 30);
                    //double minPrice = getMinPrice(klines, i+2, 30);
                    //3天前价格
                    double p3 = Double.parseDouble(klines.get(klines.size() - i - 2).toString().split(",")[2]);
                    //double v2 = (maxPrice - minPrice) / p3;
                    //蛟龙出海战法
                    // 一阳穿三线 5 10  30
                    //if (bb && b0 && /*price && money &&price && money &&*/ v2 < 9.8 && v2 >-9.8) {
                    //低位横盘，缓慢上涨几天
                    // if (b && b1 /*&& price && money*/ && v2 < 9.8 && v2 > -9.8) {
                    //if(v2<0.05&& v < 9.8 && v > -9.8&&f1>te1&&price){
                    //一阳穿四线30 5 10  20 30
                    // if (b2 && b3 /*&& price && money*/ && v2 < 9.8 && v2 > -9.8) {
                    //一阳穿四线60 5 10 20 60
                    //if (b4 && b5 /*&& price && money*/ && v2 < 9.8 && v2 > -9.8) {
                    //一阳穿五线 5 10 20 30 60
                    //if (/*b6 && b7 &&*/f1>te1&& price && money && v < 9.8 && v > 2) {
                    //if (/*b6 && b7 &&*/price && money && v < 9.8 && v > -9.8) {
                    //if (/*b6 && b7 &&*//*f1>te1&&v9<0.1&& v7>2&&price && money*/ b8&&b9&& v < 9.8 && v > -9.8) {
                    //if(v2<0.05/*&&v7>3*/&&s>maxPrice/*&&s==g*/&& v < 9.8 && v > -9.8&&price){
                    //if (flag ==2 && s == g /*&& money*/ && v < 9.8 && v > -9.8) {
                    //if (flag ==3 && s == g /*&& money*/ && v < 9.8 && v > -9.8) {
                    boolean price0 = getPrice0(klines, i);
                    //if (flag >1&&flag<4 && s == g /*&&v6>0.02*/ /*&& money*/&&price0 && v < 9.8 && v >0) {
                    // if(vv3>9.8&&v<0&&vv1<0&&vv2<0){
                    //if(b8&&b9 && v < 9.8 && v >0){
                    if (b10/*||b11||b12||b13||b14||b15||b16||b17||b18||b19*/ /*&& v < 9.8 && v > 0&&(v8/v61)>5*/) {
                        boolean price1205 = getPrice1205(klines, i);

                        //上影线
                        double v2;
                        //下影线
                        double v7;

                        //实体部分
                        double v12;
                        if (s > k || s == k) {
                            v2 = (g - s) / k;
                            v7 = (k - d) / k;
                            v12 = (s - k) / k;
                        } else {
                            v2 = (g - k) / k;
                            v7 = (s - d) / k;
                            v12 = (k - s) / k;
                        }


                        //低位十字星
                        //if(price1205/*&&v2>0.01&&v7>0.01*/&&v12<0.005){
                        //次日涨幅
                        double v11 = Double.parseDouble(klines.get(klines.size() - i + 1).toString().split(",")[8]);
                        allupnum += v11;
                        allup++;
                        if (v11 > 0 || v11 == 0) {
                            numu++;
                        } else {
                            numd++;
                        }


                        //当天收盘
                        double vs0 = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[2]);
                        //第3天收盘价
                        double vs03 = Double.parseDouble(klines.get(klines.size() - i + 3).toString().split(",")[2]);
                        double v3 = (vs03 - vs0) / vs0;

                        //第5天收盘价
                        /*double vs05 = Double.parseDouble(klines.get(klines.size() - i + 5).toString().split(",")[2]);
                        double v5 = (vs05 - vs0) / vs0;

                        //第10天收盘价
                        double vs010 = Double.parseDouble(klines.get(klines.size() - i + 10).toString().split(",")[2]);
                        double v10 = (vs010 - vs0) / vs0;

                        //第20天收盘价
                        double vs020 = Double.parseDouble(klines.get(klines.size() - i + 20).toString().split(",")[2]);
                        double v20 = (vs020 - vs0) / vs0;
*/
                        sum += v11;
                        sum3 += v3;
                     /*  sum5 += v5;
                        sum10 += v10;
                        sum20 += v20;*/

                        num0++;


                        if (vs03 > vs0) {
                            numu3++;
                        } else {
                            numd3++;
                        }

                       /* if (vs05 > vs0) {
                            numu5++;
                        } else {
                            numd5++;
                        }

                        if (vs010 > vs0) {
                            numu10++;
                        } else {
                            numd10++;
                        }

                        if (vs020 > vs0) {
                            numu20++;
                        } else {
                            numd20++;
                        }*/


                        if (!dateList.contains(klines.get(klines.size() - i).toString().split(",")[0])) {
                            dateList.add(klines.get(klines.size() - i).toString().split(",")[0]);
                        }
                        if (v11 > 9.8) {
                            ban++;
                        }


                        System.out.println(name + " : " + klines.get(klines.size() - i).toString().split(",")[0] + "  次日涨幅" + v11);
                    }

                    //近120天成交量处于高位
                  /*  boolean money = getMoney(klines, i);

                    //低位放量
                    if(price&&moneyUp&&v2<9.8&&v2>-9.8){
                        //次日涨幅
                        double v = Double.parseDouble(klines.get(klines.size()  - i+1).toString().split(",")[8]);
                        allupnum+=v;
                        allup++;
                        if(v>0||v==0){
                            numu++;
                        }else {
                            numd++;
                        }
                        System.out.println(name+" : "+klines.get(klines.size()  - i).toString().split(",")[0]+"  次日涨幅"+ v );
                    }
*/
                }
            }
        }
    }


    public static String getWebData(String code, int type) {
        String data = null;
        try {
            //百度股票接口
            //String url = "https://finance.pae.baidu.com/vapi/v1/getquotation?group=quotation_kline_ab&code="+code+"&ktype=day";
            // 连接到新浪财经接口
            String url = "https://push2his.eastmoney.com/api/qt/stock/kline/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f116,f117&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&beg=0&end=20500101&rtntype=6&secid=" + type + "." + code + "&klt=101&fqt=1";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Cookie", "qgqp_b_id=488de58836c5f30c189b9ad4c700b9f5; st_si=04137509447995; websitepoptg_api_time=1734056942833; st_asi=delete; HAList=ty-1-603999-%u8BFB%u8005%u4F20%u5A92%2Cty-0-002888-%u60E0%u5A01%u79D1%u6280%2Cty-0-002269-%u7F8E%u90A6%u670D%u9970%2Cty-0-000813-%u5FB7%u5C55%u5065%u5EB7%2Cty-0-002045-%u56FD%u5149%u7535%u5668%2Cty-0-300059-%u4E1C%u65B9%u8D22%u5BCC%2Cty-1-688605-N%u5148%u950B%2Cty-1-000001-%u4E0A%u8BC1%u6307%u6570; st_pvi=62081555482493; st_sp=2024-12-11%2010%3A26%3A42; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink; st_sn=60; st_psi=20241213164756225-113200301201-5528754653");
            // 设置连接超时时间为 10 秒
            con.setConnectTimeout(10000);
            // 设置读取超时时间为 10 秒
            con.setReadTimeout(10000);
            // 从数据源获取实时股票数据
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            con.disconnect();
            data = response.toString();
        } catch (Exception e) {
            //throw new RuntimeException(e);
            //e.printStackTrace();
        }

        return data;
    }


    //蛟龙出海战术
    public static void JiaolongSetSail(JSONObject jsonObject) {
        String name = jsonObject.get("name").toString();
        JSONArray klines = JSONArray.parse(jsonObject.get("klines").toString());
        if (klines.size() > 161) {
            for (int i = 2; i < 100; i++) {
                //蛟龙出海战法
                //昨日涨幅
                double v2 = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[8]);
                //次日涨幅
                double v3 = Double.parseDouble(klines.get(klines.size() - i + 1).toString().split(",")[8]);
                JSONObject lines = getLines(klines, i);
                double f1 = Double.parseDouble(lines.get("f1").toString());
                double te1 = Double.parseDouble(lines.get("te1").toString());
                double tw1 = Double.parseDouble(lines.get("tw1").toString());
                double th1 = Double.parseDouble(lines.get("th1").toString());
                double six1 = Double.parseDouble(lines.get("six1").toString());
                //开盘价
                double k = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[1]);
                //收盘价
                double s = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[2]);
                boolean b = k < f1 && k < te1 && k < tw1 && k < th1;
                boolean b1 = s > f1 && s > te1 && s > tw1 && s > th1;
                if (b && b1) {
                    //次日涨幅
                    double v = Double.parseDouble(klines.get(klines.size() - i + 1).toString().split(",")[8]);
                    allupnum += v;
                    allup++;
                    if (v > 0 || v == 0) {
                        numu++;
                    } else {
                        numd++;
                    }
                    System.out.println(name + " : " + klines.get(klines.size() - i).toString().split(",")[0] + "  次日涨幅" + v);
                }

            }
        }
    }
}

